package com.lcl.jarvis.bo.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcl.jarvis.entity.DoctorsDO;
import com.lcl.jarvis.dto.DoctorsDtoAssembler;
import com.lcl.jarvis.dao.DoctorsDAO;
import com.lcl.jarvis.bo.DoctorsBO;

import com.lcl.jarvis.dto.DoctorsQueryDTO;
import com.lcl.jarvis.dto.DoctorsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author licl
 * @since 2024-05-16
 */
@Service
@Slf4j
public class DoctorsBOImpl extends ServiceImpl<DoctorsDAO,DoctorsDO> implements DoctorsBO {

    private final DoctorsDAO doctorsDAO;


    @Autowired
    public DoctorsBOImpl(DoctorsDAO doctorsDAO) {
        this.doctorsDAO = doctorsDAO;
    }


    /**
     * 分页查询信息
     *
     * @param pageNumber 页码
     * @param pageSize 每页数量
     * @param sort 正序/倒序
     * @param order 排序字段-属性名
     * @param doctorsDto 参数 DTO 对象
     * @return 分页对象
     */
    @Override
    public Map<String, Object> queryByDTOPage(DoctorsQueryDTO doctorsQueryDto) {

        //调用中台或DB
    	IPage<DoctorsDO> page = new Page(doctorsQueryDto.getCurrentPage(), doctorsQueryDto.getPageSize());
    	IPage<DoctorsDO> da = doctorsDAO.baseQueryByDTO(page, doctorsQueryDto);
    	List<DoctorsDTO> doctorsList = DoctorsDtoAssembler.toDoctorsDtoList(da.getRecords());

    	Map<String,Object> rs = new HashMap<String, Object>();
    	rs.put("currentPage", da.getCurrent());
    	rs.put("pageSize", da.getSize());
    	rs.put("total", da.getTotal());
    	rs.put("result", doctorsList);
    	return rs;

	}

    /**
     * 分页查询信息
     *
     * @param doctorsDto 参数 DTO 对象
     * @return 分页对象
     */
    @Override
    public List<DoctorsDTO> queryByDTO(DoctorsQueryDTO doctorsDto) {
        List<DoctorsDO> doctorsList = doctorsDAO.baseQueryByDTO(doctorsDto);
        return DoctorsDtoAssembler.toDoctorsDtoList(doctorsList);
    }

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return Doctors
     */
    @Override
    public DoctorsDTO getById(String id) {
        DoctorsDO doctors = doctorsDAO.selectById(id);
        return DoctorsDtoAssembler.toDoctorsDto(doctors);
    }

    /**
     * 添加
     *
     * @param doctorsDto DoctorsDTO
     * @return DoctorsDTO
     */
    @Override
    public DoctorsDTO save(DoctorsDTO doctorsDto) {
        DoctorsDO doctors = DoctorsDtoAssembler.toDoctorsDo(doctorsDto);
        doctorsDAO.insert(doctors);
        return DoctorsDtoAssembler.toDoctorsDto(doctors);
    }


    /**
     * 更新
     *
     * @param doctorsDto DoctorsDTO
     * @return DoctorsDTO
     */
    @Override
    public DoctorsDTO updateById(DoctorsDTO doctorsDto) {
        DoctorsDO doctors = DoctorsDtoAssembler.toDoctorsDo(doctorsDto);
        doctorsDAO.updateById(doctors);
        return DoctorsDtoAssembler.toDoctorsDto(doctors);
    }

    /**
     * 物理删除
     *
     * @param id 主键
     */
    @Override
    public void delete(String id) {
        doctorsDAO.deleteById(id);
    }

}
